#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_device.h"

#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"

#include "hi_io.h"
#include "hi_uart.h"
#include "iot_gpio.h"
#include "base_connect.h"
#include "base_FSR.h"

// 主函数
static void WifiConnectTask(void *arg)
{
    (void)arg;
    int ret;
    TCP_WIFI_Init();       // WIFI设置
    FSR_Init(Device_Flag); // 串口以及设备号初始化
    /****  SOCKET    ****/
    struct sockaddr_in server_sock, clinet_sock;
    int sock_fd, client_fd;
    int sin_size;
    if (sock_fd = socket(AF_INET, SOCK_STREAM, 0) == -1)
    {
        printf("socket error\n");
        exit(1);
    }

    bzero(&server_sock, sizeof(server_sock));
    server_sock.sin_family = AF_INET;
    server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
    server_sock.sin_port = htons(TCP_PORT);

    if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
    {
        printf("bind error\n");
        exit(1);
    }

    if (listen(sock_fd, TCP_BACKLOG) == -1)
    {
        printf("listen error\n");
        exit(1);
    }
    printf("start accept\n");

    while (1)
    {
        sin_size = sizeof(struct sockaddr_in);
        if ((client_fd = accept(sock_fd, (struct sockaddr *)&clinet_sock, (socklen_t *)&sin_size)) == -1)
        {
            printf("accept error\n");
            continue;
        }
        printf("accept success\n");
        while (1)
        {
            //获取指令:
            if ((ret = recv(client_fd, TCP_RX_BUF, BUF_MAX, 0)) <= 0)
            {
                printf("recv error\n");
                break;
            }
            printf("ret=%d,recv:%s\n", ret, TCP_RX_BUF);
            //解析指令
            FSR_ReceiveHandle();
            //执行指令:
            if (strncmp(pRecvFSR->Status,"inquire",7)==0)
            {
                hi_uart_read_timeout(HI_UART_IDX_2, (unsigned char *)UART_RX_BUF, BUF_MAX,0xff); // hi_uart_read_timeout
                for (int i = 0; i < 10;i++)
                    printf("%x ", UART_RX_BUF[i] & 0xff);
                FSR_UART_ReceiveHandle();
                FSR_TransmitHandle();
                send(client_fd, TCP_TX_BUF, strlen(TCP_TX_BUF) + 1, 0);
            }
            else if (strncmp(pRecvFSR->Status,"start",5)==0)
            {
                memset(pSendFSR->Status, 0, 10);
                strcat(pSendFSR->Status, "start");
                FSR_UART_ToFLY(FLY_Start);
            }else if (strncmp(pRecvFSR->Status,"stop",4)==0)
            {
                memset(pSendFSR->Status, 0, 10);
                strcat(pSendFSR->Status, "stop");
                FSR_UART_ToFLY(FLY_Stop);
            }
        }
    }
}

static void WifiConnectDemo(void)
{
    osThreadAttr_t attr;
    printf("[TCP_UART] TCP_UART_Task()\n");

    attr.name = "WifiConnectTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 10240;
    attr.priority = osPriorityNormal;

    if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL)
    {
        printf("[WifiConnectDemo] Falied to create WifiConnectTask!\n");
    }
}

APP_FEATURE_INIT(WifiConnectDemo);
